package edu.service.impl;

import edu.service.ScoreService;
import edu.vo.Score;
import edu.vo.ID.ScoreId;
import edu.repository.ScoreRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ScoreServiceImpl implements ScoreService {

    private final ScoreRepository scoreRepository;

    public ScoreServiceImpl(ScoreRepository scoreRepository) {
        this.scoreRepository = scoreRepository;
    }

    @Override
    public Score saveScore(Score score) {
        return scoreRepository.save(score);
    }

    @Override
    public Optional<Score> getScoreById(ScoreId id) {
        return scoreRepository.findById(id);
    }

    @Override
    public List<Score> getAllScores() {
        return scoreRepository.findAll();
    }

    @Override
    public void deleteScoreById(ScoreId id) {
        scoreRepository.deleteById(id);
    }

    @Override
    public Score updateScore(ScoreId id, Score score) {
        return scoreRepository.findById(id)
                .map(existingScore -> {
                    existingScore.setScore(score.getScore());
                    existingScore.setResult(score.getResult());
                    existingScore.setCreatedAt(score.getCreatedAt());
                    existingScore.setUpdatedAt(score.getUpdatedAt());
                    return scoreRepository.save(existingScore);
                })
                .orElse(null);
    }
}